const app = getApp();
var api = require("../../api/api.js");
var utils = require("../../utils/utils.js");
var event = require("../../event/event.js");
var storageManage = require("../../manage/storageManage.js");
var imgUp = require("../../template/imgUp/imgUp.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    clientData: {},
    addrData: {},
    sukList: [{
      goods_main_index: 0,
      goods_main_id: 0,
      goods_minor_index: 0,
      goods_minor_id: 0,
      count: 1
    }, {
      goods_main_index: 0,
      goods_main_id: 0,
      goods_minor_index: 0,
      goods_minor_id: 0,
      count: 1
    }],
    goods_list: [{
      name: '纸尿裤',
      id: '1',
      suk: [{
        name: "纸尿裤xl",
        id: '11'
      }, {
        name: "纸尿裤x",
        id: '12'
      }]
    }, {
      name: "拉拉裤",
      id: '2',
      suk: [{
        name: "拉拉裤xl",
        id: '21'
      }, {
        name: "拉拉裤x",
        id: '22'
      }]
    }, {
      name: "湿纸巾",
      id: '3',
      suk: [{
        name: "湿纸巾xl",
        id: '31'
      }, {
        name: "湿纸巾x",
        id: '32'
      },]
    }],

    maxLength: 3,
    imglist: [],
    isAddBtnShow: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options.state == "return") {
      wx.setNavigationBarTitle({
        title: '退货申请'
      })

    } else {
      wx.setNavigationBarTitle({
        title: '换货申请'
      })
    }
    that.setData({
      state: "options.state"
    })
    imgUp.init.apply(this, []);//引入方法
    event.on('addressClient', this, function (data) {
      that.setData({
        clientData: data,
        addrData: data
      })
    });
  },
  addressClient: utils.throttle(function () {
    wx.navigateTo({
      url: '../addressClient/addressClient'
    })
  }),
  //一级排序方式
  goodsMainChange: function (e) {
    var index = e.detail.value;
    var parentIndex = e.currentTarget.dataset.idx;
    var sukItem_index_str = 'sukList[' + parentIndex + '].goods_main_index';
    var sukItem_id_str = 'sukList[' + parentIndex + '].goods_main_id';
    this.setData({
      [sukItem_index_str]: index,
      [sukItem_id_str]: this.data.goods_list[index].id
    })
  },
  //二级排序方式
  goodsMinorChange: function (e) {
    var index = e.detail.value;
    var parentIndex = e.currentTarget.dataset.idx;
    var minor_id = this.data.goods_list[this.data.sukList[parentIndex].goods_main_index].suk[index].id;
    var sukItem_index_str = 'sukList[' + parentIndex + '].goods_minor_index';
    var sukItem_id_str = 'sukList[' + parentIndex + '].goods_minor_id';

    this.setData({
      [sukItem_index_str]: index,
      [sukItem_id_str]: minor_id
    })
  },
  countChanges:function(e){
    var count = e.detail.value;
    var parentIndex = e.currentTarget.dataset.idx;
    var itemCount = 'sukList[' + parentIndex + '].count';
    this.setData({
      [itemCount]: count
    })
  },
  subSukList: utils.throttle(function(e){
    var that=this;
    var parentIndex = e.currentTarget.dataset.idx;
    var sukList = that.data.sukList;
    sukList.splice(parentIndex,1);
    that.setData({
      sukList: sukList
    })
  }),
  addSukList:utils.throttle(function(e){
    var that = this;
    var sukList = that.data.sukList;
    sukList.push({
      goods_main_index: 0,
      goods_main_id: 0,
      goods_minor_index: 0,
      goods_minor_id: 0,
      count: 1
    })
    that.setData({
      sukList: sukList
    })
  }),
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})